﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Systems.Service;
using ShareMarking.Service;
using ShareMarking.Entity;

namespace ShareMarking.Web.admin.EnterPrise.QrcodeType
{
    public partial class Action : ManagePage
    {
        #region 属性=====================================

        /// <summary>
        /// 类型
        /// </summary>
        protected String Par_QT_Name {
            get { return txt_QT_Name.Text;}
            set { txt_QT_Name.Text = value; }
        }

        /// <summary>
        /// 描述
        /// </summary>
        protected String Par_QT_Desc {
            get { return txt_QT_Desc.Text;}
            set { txt_QT_Desc.Text = value; }
        }

        /// <summary>
        /// 总数
        /// </summary>
        protected Int32 Par_QT_Count {
            get {
                if (txt_QT_Count.Text == "")
                    { return 0;}
                else
                    { return txt_QT_Count.Text.ConvertTo<Int32>();}
            }
            set { txt_QT_Count.Text = value.ToString(); }
        }

        /// <summary>
        /// 开始数字
        /// </summary>
        protected Int32 Par_QT_StartIndex {
            get {
                if (txt_QT_StartIndex.Text == "")
                    { return 0;}
                else
                    { return txt_QT_StartIndex.Text.ConvertTo<Int32>();}
            }
            set { txt_QT_StartIndex.Text = value.ToString(); }
        }

        /// <summary>
        /// 结束数字
        /// </summary>
        protected Int32 Par_QT_EndIndex {
            get {
                if (txt_QT_EndIndex.Text == "")
                    { return 0;}
                else
                    { return txt_QT_EndIndex.Text.ConvertTo<Int32>();}
            }
            set { txt_QT_EndIndex.Text = value.ToString(); }
        }

        /// <summary>
        /// 使用个数
        /// </summary>
        protected Int32 Par_QT_Used {
            get {
                if (txt_QT_Used.Text == "")
                    { return 0;}
                else
                    { return txt_QT_Used.Text.ConvertTo<Int32>();}
            }
            set { txt_QT_Used.Text = value.ToString(); }
        }

        /// <summary>
        /// 剩余个数
        /// </summary>
        protected Int32 Par_QT_Surplus {
            get {
                if (txt_QT_Surplus.Text == "")
                    { return 0;}
                else
                    { return txt_QT_Surplus.Text.ConvertTo<Int32>();}
            }
            set { txt_QT_Surplus.Text = value.ToString(); }
        }

        /// <summary>
        /// 模板
        /// </summary>
        protected String Par_QT_Template {
            get {return txt_QT_Template.Text;}
            set { txt_QT_Template.Text = value; }
        }

        /// <summary>
        /// 用户
        /// </summary>
        protected String Par_QT_UserName {
            get { return txt_QT_UserName.Text;}
            set { txt_QT_UserName.Text = value; }
        }

        /// <summary>
        /// 秘钥
        /// </summary>
        protected String Par_QT_SecretKey {
            get { return txt_QT_SecretKey.Text;}
            set { txt_QT_SecretKey.Text = value; }
        }

        #endregion

        #region 初始化===================================

        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!qr_QrCodeTypeService.Instance.Get_IsExist_ByWhere(qr_QrCodeType._.QrCodeTypeID == this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.View.ToString()); //检查权限

                //绑定下拉框

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #endregion

        #region 绑定下拉框===============================
        

        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var entity = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == _id);
            if(entity == null)return;
            Par_QT_Name = entity.QT_Name;
            Par_QT_Desc = entity.QT_Desc;
            Par_QT_Count = entity.QT_Count;
            Par_QT_StartIndex = entity.QT_StartIndex;
            Par_QT_EndIndex = entity.QT_EndIndex;
            Par_QT_Used = entity.QT_Used.HasValue?entity.QT_Used.Value:0;
            Par_QT_Surplus = entity.QT_Surplus.HasValue?entity.QT_Surplus.Value:0;
            Par_QT_Template = entity.QT_Template;
            Par_QT_UserName = entity.QT_UserName;
            Par_QT_SecretKey = entity.QT_SecretKey;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            var entity = new qr_QrCodeType();
            entity.QT_Name = Par_QT_Name;
            entity.QT_Desc = Par_QT_Desc;
            entity.QT_Count = Par_QT_Count;
            entity.QT_StartIndex = Par_QT_StartIndex;
            entity.QT_EndIndex = Par_QT_EndIndex;
            entity.QT_Used = Par_QT_Used;
            entity.QT_Surplus = Par_QT_Surplus;
            entity.QT_Template = Par_QT_Template;
            entity.QT_UserName = Par_QT_UserName;
            entity.QT_SecretKey = Par_QT_SecretKey;
            entity.CreateOn = DateTime.Now;
            entity.ModifiedOn = DateTime.Now;

            var result = qr_QrCodeTypeService.Instance.Add_Entity(entity);

            if (result)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加qr_QrCodeType:" + entity.QrCodeTypeID); //记录日志
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            var entity = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == _id);
            if (entity == null) return false;
            entity.QT_Name = Par_QT_Name;
            entity.QT_Desc = Par_QT_Desc;
            entity.QT_Count = Par_QT_Count;
            entity.QT_StartIndex = Par_QT_StartIndex;
            entity.QT_EndIndex = Par_QT_EndIndex;
            entity.QT_Used = Par_QT_Used;
            entity.QT_Surplus = Par_QT_Surplus;
            entity.QT_Template = Par_QT_Template;
            entity.QT_UserName = Par_QT_UserName;
            entity.QT_SecretKey = Par_QT_SecretKey;
            entity.ModifiedOn = DateTime.Now;

            var result = qr_QrCodeTypeService.Instance.Update_Entity(entity);

            if (result)
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改qr_QrCodeType:" + entity.QrCodeTypeID); //记录日志
            }
            return result;
        }
        #endregion

        #region 保存按钮事件=============================

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString())
            { //修改
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("修改成功！", "Index.aspx", "Success");
            }
            else //添加
            {
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("添加成功！", "Index.aspx", "Success");
            }
        }

        #endregion
    }

}